'use strict';
import request from 'request';

export default class extends think.controller.base {
  /**
   * some base method in here
   */
  async __before() {

    let log = {};
    log.id = null;
    log.type - 'visit';
    log.ipAddress = this.ip();
    log.userAgent = this.userAgent();
    log.url = this.http.url;
    log.createTime = think.datetime();

    if (this.ip() != '127.0.0.1') {

      let utilService = think.service('util');
      let utilServiceInstance = new utilService();

      // let info = await utilServiceInstance.getIpinfo(this.ip());
      utilServiceInstance.getIpinfo(this.ip()).then(function(info) {
        log.country = info.country;
        log.area = info.area;
        log.region = info.region;
        log.city = info.city;
        log.county = info.county;

        //写入日志
        request.post('http://127.0.0.1:8360/api/log/add', {
          form: {
            log: JSON.stringify(log)
          }
        }, function(err, httpResponse, body) {
          if (err) {
            think.error(err);
          } else {
            think.log(body);
          }
        });
      }).catch(function(err) {
        think.error('出错了' + err);
      });
    }
  }
}